package com.javaweb.dao.impl;

import com.javaweb.dao.CommentDao;
import com.javaweb.model.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.ObjectUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;

@Repository
public class CommentDaoImpl implements CommentDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public int insert(Comment newComment) {
        if (ObjectUtils.isEmpty(newComment) || ObjectUtils.isEmpty(newComment.getArticleId())) {
            return 0; // Insert failed
        }

        String sql = "INSERT INTO tb_comment (article_id, user_id, content, create_time, support, oppose) VALUES (?, ?, ?, ?, 0, 0)";
        Object[] args = new Object[]{
                newComment.getArticleId(),
                newComment.getUserId(),
                newComment.getContent(),
                new java.sql.Date(newComment.getCreateTime().getTime())
        };

        return jdbcTemplate.update(sql, args) > 0 ? 1 : 0; // Return insert result
    }

    @Override
    public int insertSupport(Integer commentId) {
        if (ObjectUtils.isEmpty(commentId)) {
            return 0; // Like failed
        }
        String sql = "UPDATE tb_comment SET support = support + 1 WHERE id = ?";
        return jdbcTemplate.update(sql, commentId) > 0 ? 1 : 0; // Return like result
    }

    @Override
    public int insertOppose(Integer commentId) {
        if (ObjectUtils.isEmpty(commentId)) {
            return 0; // Dislike failed
        }
        String sql = "UPDATE tb_comment SET oppose = oppose + 1 WHERE id = ?";
        return jdbcTemplate.update(sql, commentId) > 0 ? 1 : 0; // Return dislike result
    }

    @Override
    public List<Comment> selectAllCommentsById(Integer articleId) {
        if (ObjectUtils.isEmpty(articleId)) {
            return Collections.emptyList(); // Return empty list
        }

        String sql = "SELECT * FROM tb_comment WHERE article_id = ? ORDER BY create_time DESC";
        return jdbcTemplate.query(sql, new Object[]{articleId}, new CommentRowMapper());
    }

    private static class CommentRowMapper implements RowMapper<Comment> {
        @Override
        public Comment mapRow(ResultSet rs, int rowNum) throws SQLException {
            Comment comment = new Comment();
            comment.setId(rs.getInt("id"));
            comment.setContent(rs.getString("content"));
            comment.setArticleId(rs.getInt("article_id"));
            comment.setUserId(rs.getInt("user_id"));
            comment.setSupport(rs.getInt("support"));
            comment.setOppose(rs.getInt("oppose"));
            comment.setCreateTime(rs.getTimestamp("create_time"));
            comment.setUpdateTime(rs.getTimestamp("update_time"));
            return comment;
        }
    }
}
